package com.kaoyaya.delivery.adapter;

import java.util.List;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.OrderItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 产品Adapter
 * @date 2013-5-26
 * @author Goven
 * @email gxl3999@gmail.com
 */
public class OrderAdapter extends BaseAdapter {

	private List<OrderItem> order;
	private LayoutInflater inflater;
	private View.OnClickListener clickListener;
	
	public OrderAdapter(Context context, View.OnClickListener clickListener, List<OrderItem> order) {
		inflater = LayoutInflater.from(context);
		this.order = order;
		this.clickListener = clickListener;
	}
	
	public void deleteOrderItem(OrderItem orderItem) {
		this.order.remove(orderItem);
	}
	
	public void deleteOrderItem(int position) {
		this.order.remove(position);
	}
	
	@Override
	public int getCount() {
		return order.size();
	}

	@Override
	public OrderItem getItem(int arg0) {
		return order.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		OrderItem orderItem = getItem(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_order, null);
			holder.ivDelete = (ImageView) convertView.findViewById(R.id.ivDelete);
			holder.ivDelete.setTag(position);
			holder.ivDelete.setOnClickListener(clickListener);
			holder.tvArea = (TextView) convertView.findViewById(R.id.tvArea);
			holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
			holder.tvNo = (TextView) convertView.findViewById(R.id.tvNo);
			holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			holder.tvType = (TextView) convertView.findViewById(R.id.tvType);
			holder.tvSum = (TextView) convertView.findViewById(R.id.tvSum);
			holder.tvTotal = (TextView) convertView.findViewById(R.id.tvTotal);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvArea.setText(orderItem.getArea());
		holder.tvName.setText(orderItem.getName());
		holder.tvNo.setText(String.valueOf(orderItem.getNo()));
		holder.tvPrice.setText(String.valueOf(orderItem.getPrice()));
		holder.tvType.setText(orderItem.getType() == 1 ? "单品" : "套餐");
		holder.tvSum.setText(String.valueOf(orderItem.getCount()));
		holder.tvTotal.setText("￥" + String.valueOf(orderItem.getCount() * orderItem.getPrice()));
		return convertView;
	}

	class ViewHolder {
		TextView tvType, tvNo, tvName, tvPrice, tvSum, tvArea, tvTotal;
		ImageView ivDelete;
	}
	
}
